package cn.iocoder.yudao.module.pypm.controller.admin.projectmember;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.pypm.controller.admin.projectmember.vo.*;
import cn.iocoder.yudao.module.pypm.dal.dataobject.projectmember.ProjectMemberDO;
import cn.iocoder.yudao.module.pypm.service.projectmember.ProjectMemberService;

@Tag(name = "管理后台 - 项目成员中间")
@RestController
@RequestMapping("/pypm/project-member")
@Validated
public class ProjectMemberController {

    @Resource
    private ProjectMemberService projectMemberService;

    @PostMapping("/create")
    @Operation(summary = "创建项目成员中间")
    @PreAuthorize("@ss.hasPermission('pypm:project-member:create')")
    public CommonResult<Long> createProjectMember(@Valid @RequestBody ProjectMemberSaveReqVO createReqVO) {
        return success(projectMemberService.createProjectMember(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目成员中间")
    @PreAuthorize("@ss.hasPermission('pypm:project-member:update')")
    public CommonResult<Boolean> updateProjectMember(@Valid @RequestBody ProjectMemberSaveReqVO updateReqVO) {
        projectMemberService.updateProjectMember(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目成员中间")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pypm:project-member:delete')")
    public CommonResult<Boolean> deleteProjectMember(@RequestParam("id") Long id) {
        projectMemberService.deleteProjectMember(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目成员中间")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pypm:project-member:query')")
    public CommonResult<ProjectMemberRespVO> getProjectMember(@RequestParam("id") Long id) {
        ProjectMemberDO projectMember = projectMemberService.getProjectMember(id);
        return success(BeanUtils.toBean(projectMember, ProjectMemberRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目成员中间分页")
    @PreAuthorize("@ss.hasPermission('pypm:project-member:query')")
    public CommonResult<PageResult<ProjectMemberRespVO>> getProjectMemberPage(@Valid ProjectMemberPageReqVO pageReqVO) {
        PageResult<ProjectMemberDO> pageResult = projectMemberService.getProjectMemberPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProjectMemberRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目成员中间 Excel")
    @PreAuthorize("@ss.hasPermission('pypm:project-member:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProjectMemberExcel(@Valid ProjectMemberPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProjectMemberDO> list = projectMemberService.getProjectMemberPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "项目成员中间.xls", "数据", ProjectMemberRespVO.class,
                        BeanUtils.toBean(list, ProjectMemberRespVO.class));
    }

}